<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\Option;
class Web extends Common{
    //网站设置模型
    protected $option;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->option = new Option();
    }
    /**
     *网站信息
     * return json
     */
    public function webInfo(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $info =$this->option->where(['option_name'=>'web_info'])->find();
        if(empty($info)){
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }else{
            $info=json_decode($info['option_value'],true);
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['info']=$info;
        }
        return $msg;
    }
    /**
     *修改网站信息
     *return json
     */
    public function editeWeb(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $site_name = $this->request->post('site_name');
        $site_icp      = $this->request->post('site_icp');
        $site_gwab     = $this->request->post('site_gwab');
       /* if(empty($site_name) || empty($site_icp) || empty($site_gwab)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }*/
        $data=[
            'site_name'  =>$site_name,
            'site_icp'  =>$site_icp,
            'site_gwab' =>$site_gwab
        ];
       $data=json_encode($data);
       $res = $this->option->save(['option_value'=>$data],['option_name'=>'web_info']);
       if($res){
           $msg['message']=checkMsg(OPERA_SUCCESS);
       }else{
           $msg['message']=checkMsg(OPERA_FAIL);
       }
       return $msg;
    }
    /**
     *SEO信息
     * return json
     */
    public function seoInfo(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $info =$this->option->where(['option_name'=>'seo_info'])->find();
        if(empty($info)){
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }else{
            $info = json_decode($info['option_value'],true);
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['info']=$info;
        }
       return $msg;
    }
 /**
  *修改seo信息
  * return json
  */
  public function editeSeoInfo(){
      if(empty($this->adminInfo)){
           $msg['message']=checkMsg(LOGIN_ERROR);
           return $msg;
      }
      $seo_name = $this->request->post('seo_name');
      $seo_keywords = $this->request->post('seo_keywords');
      $seo_desc = $this->request->post('seo_desc');
     /* if(empty($seo_name) || empty($seo_keywords) || empty($seo_desc)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }*/
      $data=[
          'seo_name' =>$seo_name,
          'seo_keywords' =>$seo_keywords,
          'seo_desc'     =>$seo_desc
      ];
      $data=json_encode($data);
      $res = $this->option->save(['option_value'=>$data],['option_name'=>'seo_info']);
      if($res){
          $msg['message']=checkMsg(OPERA_SUCCESS);
      }else{
          $msg['message']=checkMsg(OPERA_FAIL);
      }
      return $msg;
  }
  /**
   *微信公众号图片
   * return json
   */
  public function wechatPublic(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $info = $this->option->where(['option_name'=>'wechat_public'])->find();
      if(empty($info)){
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }else{
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['path'] =$info['option_value'];
      }
      return $msg;
  }
  /**
   *修改微信公众号图片
   * return json
   */
  public function editeWechatPublic(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $path = $this->request->post('path');
      $res = $this->option->save(['option_value'=>$path],['option_name'=>'wechat_public']);
      if($res){
          $msg['message']=checkMsg(OPERA_SUCCESS);
      }else{
          $msg['message']=checkMsg(OPERA_FAIL);
      }
      return $msg;
  }
}